package main

import (
	"fmt"
	"github.com/panjf2000/ants/v2"
	"sync"
	"time"
)

func main() {

	start := time.Now()
	defer ants.Release()
	var wg sync.WaitGroup

	// 定义任务函数
	task := func(i interface{}) {
		defer wg.Done()
		n := i.(int)
		fmt.Printf("processing task %d \n", n)
		// print running task
		fmt.Println("running task", ants.Running()) // 0
		time.Sleep(time.Second * 1)
	}

	// 创建容量为10的池
	p, _ := ants.NewPoolWithFunc(10, task)
	defer p.Release() // 释放资源
	// submit tasks
	for i := 0; i < 10; i++ {
		wg.Add(1)

		_ = p.Invoke(i)
	}
	wg.Wait()
	end := time.Now()
	// 打印耗时
	fmt.Println(end.Sub(start))
	fmt.Println("all task done", ants.Running())
}
